import user from '../../../utils/user'
const app = getApp()
import {recharge, getInsuranceProgramme } from '../../../api/app'
Page({
    data: {
        yearList: [],
        yearIndex: '0'
    },
    onLoad (options) {

    },
    onShow(){
        this.getInsuranceProgramme()
    },
    // 切换份
    onYearIndex (event) {
        const { index } = event.currentTarget.dataset
        this.setData({
            yearIndex:index
        })
    },
    // 根据参数获取内容
    async getInsuranceProgramme () {
        const result = await getInsuranceProgramme({
            dictType:'ele_sign_plan'
        })
        console.log(result, 'result')
        result.forEach((item,index)=>{
            result[index].singleAmount = (item.dictValue / item.dictLabel).toFixed(1)
        })
        this.setData({
            yearList: result
        })
        console.log(this.data.yearList, 'yearListyearList')
    },
    // 立即购买 电子签
    onEleSignRecharge () {
        
        recharge({
            useWay: 'EleRecharge',
            description: '账户充值',
            amount: {
                total: this.data.yearList[this.data.yearIndex].dictValue
            },
            attach: JSON.stringify({chgAmt:this.data.yearList[this.data.yearIndex].dictLabel}),
            payer: {
                openid: user.openid
            }
        }).then(res=>{
            app.toast('续费成功')
            res.package = res.packageValue
            wx.requestPayment(res).then(()=>{
                wx.navigateTo({
                    url: '/pages/success/success?title=充值成功&content=您可以稍后查看您的账户余额&opinion=0&back=2'
                })
            })
        })
    }
})